﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Language;

namespace Portal.CDSP
{
    public class PagingCls
    {
        public string GetPaging(string TabModuleId, int MaxItems, int ItemsPerRow, int NoOfVisiblePage, int CurrentPage, string JumpJavaScript, ref int StartIndex, ref int EndIndex)
        {
            int TotalPage = MaxItems / ItemsPerRow;
            if (MaxItems % ItemsPerRow != 0) TotalPage++;

            if (TotalPage <= 1)
            {
                StartIndex = 0;
                EndIndex = MaxItems;
                return "";
            }

            StartIndex = ItemsPerRow * CurrentPage;
            EndIndex = StartIndex + ItemsPerRow;
            if (EndIndex > MaxItems) EndIndex = MaxItems;

            int TotalSegment = TotalPage / NoOfVisiblePage;
            if (TotalPage % NoOfVisiblePage != 0) TotalSegment++;
            //doan nay tinh xem no nam o segment may
            int CurrentSegment = 0;
            for (int iSegment = 0; iSegment < TotalSegment; iSegment++)
            {
                int FromPage = iSegment * NoOfVisiblePage;
                int EndPage = FromPage + NoOfVisiblePage;
                if (EndPage > TotalPage) EndPage = TotalPage;
                if (CurrentPage >= FromPage && CurrentPage < EndPage)
                {
                    CurrentSegment = iSegment;
                    break;
                }
            }

            int StartPageIndex = CurrentSegment * NoOfVisiblePage;
            int EndPageIndex = StartPageIndex + NoOfVisiblePage;
            if (EndPageIndex > TotalPage) EndPageIndex = TotalPage;
            string Html = "";
                   
            if (CurrentPage > 0)
            {
                Html += "<li class=\"first\"><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + ",0,'" + JumpJavaScript + "');\">" + ResourceBLL.GetText("paging", "First", "đầu") + "</a></li>";
                Html += "<li class=\"prev\"><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + (CurrentPage - 1) + ",'" + JumpJavaScript + "');\">" + ResourceBLL.GetText("paging", "Preview", "trước") + "</a></li>";
            }

            for (int iIndex = StartPageIndex; iIndex < EndPageIndex; iIndex++)
            {
                if (iIndex != CurrentPage)
                {
                    Html += "<li><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + iIndex + ",'" + JumpJavaScript + "');\">" + (iIndex + 1) + "</a></li>";
                }
                else
                {
                    Html += "<li class=\"current\"><a  href=\"#\">[" + (iIndex + 1) + "]</a></li>";
                }
            }
            if (CurrentPage < TotalPage - 1)
            {
                Html += "<li class=\"next\"><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + (CurrentPage + 1) + ",'" + JumpJavaScript + "');\">" + ResourceBLL.GetText("paging", "Next", "sau") + "</a></li>";
                Html += "<li class=\"end\"><a href=\"javascript:" + JumpJavaScript + "('" + TabModuleId + "'," + MaxItems + "," + ItemsPerRow + "," + NoOfVisiblePage + "," + (TotalPage - 1) + ",'" + JumpJavaScript + "');\">" + ResourceBLL.GetText("paging", "End", "cuối") + "</a></li>";
            }
            return Html;
        }
    }
}
